//
//  RCMessageDetailDataModel.m
//  RocketProject
//
//  Created by chen jiedan on 13-11-14.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMessageDetailDataModel.h"
#import "KFObjectExtension.h"
#import "GloableEngineService.h"


@implementation RCMessageDetailItemDataModel

@synthesize username = _username;
@synthesize content  = _content;
@synthesize avatarUrl= _avatarUrl;
@synthesize dateTime = _dateTime;
@synthesize isMy     = _isMy;

- (void) dealloc
{
    KF_RELEASE_SAFELY(_username);
    KF_RELEASE_SAFELY(_content);
    KF_RELEASE_SAFELY(_avatarUrl);
    KF_RELEASE_SAFELY(_dateTime);
    [super dealloc];
}

@end


@implementation RCMessageDetailDataModel


// 将JSON数据解析为数据模型
+(id) parseDataFromJSON:(NSDictionary*)data
{
    // 输入参数判断
    if ( nil == data )
        return nil;
    NSMutableArray *listItems = [[NSMutableArray alloc] init];
    
    RCMessageDetailDataModel *retObject = [[[RCMessageDetailDataModel alloc] init] autorelease];
    NSDictionary *dataDic = [data dictionaryValueForKey:@"data" defaultValue:nil];
    retObject.pn     = [dataDic intValueForKey:@"pn" defaultValue:0];
    retObject.rn     = [dataDic intValueForKey:@"rn" defaultValue:0];
    retObject.total  = [dataDic intValueForKey:@"total" defaultValue:0];
    
    NSString *myAvatar = [dataDic stringValueForKey:@"avatar" defaultValue:@"" operation:NSStringOperationTypeNone];
    NSString *otherAvatar = [dataDic stringValueForKey:@"avatar_read" defaultValue:@"" operation:NSStringOperationTypeNone];
    
    NSArray *dataItems = [dataDic arrayValueForKey:@"list" defaultValue:nil];
    if (nil == dataItems) {
        dataItems = [dataDic arrayValueForKey:@"items" defaultValue:nil];
    }
    
    for (NSDictionary *dic in dataItems) {
        RCMessageDetailItemDataModel *item = [[RCMessageDetailItemDataModel alloc] init];
        item.dateTime = [dic stringValueForKey:@"datetime" defaultValue:@"" operation:NSStringOperationTypeNone];
        item.content = [dic stringValueForKey:@"content" defaultValue:@"" operation:NSStringOperationTypeNone];
        item.username = [dic stringValueForKey:@"username" defaultValue:@"" operation:NSStringOperationTypeNone];
        if ([[[GloableEngineService sharedInstance] nickName] isEqualToString:item.username]) {
            item.avatarUrl = myAvatar;
            item.isMy = YES;
        }else{
            item.isMy = NO;
            item.avatarUrl = otherAvatar;
        }
        [listItems addObject:item];
        [item release];
    }
    retObject.listItems = listItems;
    [listItems release];
    return retObject;
}

@end
